package bb.lanxing.util.pop;

import android.app.Activity;
import android.app.Dialog;

import java.lang.ref.WeakReference;

public class PopDialog implements Comparable<PopDialog> {
    private Dialog dialog;
    private DialogListener dialogListener;
    private long dismissDelay;
    private long endDate;
    private WeakReference<Activity> mContext;
    private int priority;
    private long startDate;
    private int uid;

    public interface DialogListener {
        void onDismiss();

        void onShow();
    }

    public PopDialog(Builder builder) {
        this.uid = 0;
        this.uid = builder.uid;
        this.priority = builder.priority;
        this.mContext = new WeakReference<>(builder.context);
        this.startDate = builder.startDate;
        this.endDate = builder.endDate;
        this.dialog = builder.dialog;
        this.dismissDelay = builder.dismissDelay;
        this.dialogListener = builder.dialogListener;
    }

    public DialogListener getDialogListener() {
        return this.dialogListener;
    }

    public int getUid() {
        return this.uid;
    }

    public int getPriority() {
        return this.priority;
    }

    public long getStartDate() {
        return this.startDate;
    }

    public long getEndDate() {
        return this.endDate;
    }

    public Dialog getDialog() {
        return this.dialog;
    }

    public Activity getContext() {
        return this.mContext.get();
    }

    public long getDismissDelay() {
        return this.dismissDelay;
    }

    @Override
    public int compareTo(PopDialog popDialog) {
        if (this.priority > popDialog.getPriority()) {
            return 1;
        }
        return this.priority < popDialog.getPriority() ? -1 : 0;
    }

    public void show() {
        if (getDialog() == null || getDialog().isShowing()) {
            return;
        }
        getDialog().show();
    }


    public static class Builder {
        private Activity context;
        private Dialog dialog;
        private DialogListener dialogListener;
        private long dismissDelay;
        private long endDate;
        private int priority;
        private long startDate;
        private int uid = 0;

        public Builder setDialogListener(DialogListener dialogListener) {
            this.dialogListener = dialogListener;
            return this;
        }

        public Builder setUid(int i) {
            this.uid = i;
            return this;
        }

        public Builder setPriority(int i) {
            this.priority = i;
            return this;
        }

        public Builder setContext(Activity activity) {
            this.context = activity;
            return this;
        }

        public Builder setStartDate(long j) {
            this.startDate = j;
            return this;
        }

        public Builder setEndDate(long j) {
            this.endDate = j;
            return this;
        }

        public Builder setDialog(Dialog dialog) {
            this.dialog = dialog;
            return this;
        }

        public Builder setDismissDelay(long j) {
            this.dismissDelay = j;
            return this;
        }

        public PopDialog build() {
            return new PopDialog(this);
        }
    }
}
